<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use services\models\OrderGoods;
use services\models\OrderInfo;
use services\models\Goods;
use services\models\ProductDetail;
use services\models\Product;
use common\models\tmall1\GSupplierInfo;

/**
 * FullCutSearch represents the model behind the search form about `common\models\discount\FullCut`.
 */
class StatisticSearch extends OrderGoods{
    /**
     * @inheritdoc
     */
    public $start_time;
    public $end_time;
    public $goodsDetail;


    public function rules()
    {
        return [
            [[ 'start_time', 'end_time'], 'safe'],
        ];
    }


    //查询订单信息
    public function search($params){
        @set_time_limit(0);                  //防止超时

        //查询阶段订单
        $strIn      = $this->getOrder($params);
        //查询时间段的所有商品
        $orderGood  = OrderGoods::find();

        //查询条件
        $orderGood->select(['order_goods.goods_id','goods_detail.goods_supply_id','order_goods.product_id','sum(order_goods.buy_number) as buy_number','order_goods.order_info_id']);
        $orderGood->joinWith('goodsDetail',true,'LEFT JOIN');
        $orderGood->andOnCondition(['in', 'order_goods.order_info_id',$strIn]);
        $orderGood->groupBy(['order_goods.product_id']);

        $dataProvider = new ActiveDataProvider([
            'query' => $orderGood,
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);

        //处理字段
        $dataProvider->setSort([
            'attributes' => [
                /* 其它字段不要动 */
                /*  下面这段是加入的 */
                /*=============*/
                'goodsDetail' => [
                    'asc' => ['goods_detail.goods_supply_id' => SORT_ASC],
                    'desc' => ['goods_detail.goods_supply_id' => SORT_DESC],
                    'label' => 'goods_supply_id'
                ],

                /*=============*/
            ]
        ]);

        //查寻相关的名称redis 购买数量
       /*if(count($orderGood) > 0){
            foreach($orderGood as $key=>&$val){
                //取商品详情
                $val['goodPic']  = Goods::findByGoodsIdFromRedis($val['goods_id'],['goods_name']);
                //取规格详情
                $val['products'] = ProductDetail::findByProductIdFromRedis($val['product_id'],['spec_desc']);
                //查询使用和购买信息
                $val['Product']  = Product::findByProductId($val['product_id']);
                //查询供应商
                $val['name']     = GSupplierInfo::find()->select(['name'])->where(['uid'=>$val['goods_supply_id']])->asArray()->one();
            }
        }*/

        return $dataProvider;
    }

    //查询阶段订单
    public static function getOrder($params){
        //1和2 代表代发货和待收货状态
        $status     = array(1,2);

        //查询时间段相关的订单
        $orderInfo  = OrderInfo::find()
            ->select('order_info_id')
            ->andOnCondition( ['between', 'create_time', $params['start_time'], $params['end_time']])
            ->andOnCondition( ['in', 'status',$status ])
            ->asArray()
            ->all();

        $strIn      = array();
        if(count($orderInfo) > 0){
            foreach($orderInfo as $key=>$val){
                $strIn[]  =$val['order_info_id'];
            }
        }
        return $strIn;
    }

    //获取供应
    public static function GSupplier($uid){
        $uid    =!empty($uid)? intval($uid):0;

        $info   = GSupplierInfo::find()->select(['name'])->where(['uid'=>$uid])->asArray()->one();
        return $info['name'];
    }

    //取规格详情
    public static function specInfo($productId,$arr= array()){

        $productId    =!empty($productId)? intval($productId): 0;

        $info   = ProductDetail::findByProductIdFromRedis($productId,['spec_desc']);
        return $info['spec_desc'];
    }

    public static function productInfo($productId,$arr= array()){

        $productId    =!empty($productId)? intval($productId): 0;

        $info   = Product::findByProductId($productId);
        return $info;
    }
}
